﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Facebook.Web;
using System.Data.SqlClient;

public partial class _Profile : LutefiskPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // Images need to have an absolute path to be displayed!
        // Annoying, I know... but I don't make the rules         
        websiteImage.ImageUrl = MasterPage.ConvertRelativeUrlToAbsoluteUrl(Page, "~/Images/website.gif");
        downloadImage.ImageUrl = MasterPage.ConvertRelativeUrlToAbsoluteUrl(Page, "~/Images/download.png");
        downloadPDFImage.ImageUrl = MasterPage.ConvertRelativeUrlToAbsoluteUrl(Page, "~/Images/downloadpdf.gif");

        // Set the Current Musician's Data to the Session UID (the musician we requested to view)
        CurrentMusician = Musician.CreateMusician(Session["uid"].ToString());

        // Display users profile
        displayProfile(CurrentMusician.UID);

        // If Musician's Website field is not blank display a side panel to visit their website
        if (CurrentMusician.HasWebsite)
        {
            websitePanel.Visible = true;
            viewWebsiteURL.Text = CurrentMusician.Website;
        }

        // If Musician has a CV display a side panel to download it.
        if (CurrentMusician.HasCV)
        {
            cvPanel.Visible = true;
        }

        // Set customized fields on profile page
        profileImage.ImageUrl = CurrentMusician.Picture;
        profileLabel.Text = profileLabel.Text.Replace("XXX", CurrentMusician.Name);
        otherVideosLabel.Text = otherVideosLabel.Text.Replace("XXX", CurrentMusician.Name);
    }

    /// <summary>
    /// Displays the User's Profile specific by the UID passed in.
    /// </summary>
    /// <param name="UID">The Unique Facebook ID.</param>
    private void displayProfile(string UID)
    {
        using (SqlConnection dbconn = new SqlConnection(Lutefisk.CONNECTION_STRING))
        {
            dbconn.Open();

            string searchString = "SELECT * FROM musicians WHERE UID=" + UID + ";";
            
            using (SqlCommand sql = new SqlCommand(searchString, dbconn))
            {
                // Retrieve Profile Information
                using (SqlDataReader reader = sql.ExecuteReader())
                {
                    //this takes care of binding for the repeater
                    ProfileRepeater.DataSource = reader;
                    ProfileRepeater.DataBind();
                }

                // Retrieve Multimedia
                using (SqlDataReader reader = sql.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        List<EmbeddedObject> multimediaList = EmbeddedObject.CreateList(reader["multimedia"].ToString());


                        // Musician does have multimedia
                        if (multimediaList.Count > 1 || (multimediaList.Count == 1 && multimediaList[0].ID != ""))
                        {
                            List<EmbeddedObject> mainMultimediaList = new List<EmbeddedObject>();
                            List<EmbeddedObject> nowPlayingMultimediaList = new List<EmbeddedObject>();

                            // now playing and main video need to have different data sources
                            mainMultimediaList.Add(multimediaList[0]);
                            nowPlayingMultimediaList.Add(multimediaList[0]);

                            // other list should contain everything but the now playing list
                            multimediaList.RemoveAt(0);

                            // Bind the now playing video 
                            NowPlayingListRepeater.DataSource = nowPlayingMultimediaList;
                            NowPlayingListRepeater.DataBind();

                            // Bind the other list
                            MultimediaListRepeater.DataSource = multimediaList;
                            MultimediaListRepeater.DataBind();

                            // Bind the main video
                            MultimediaRepeater.DataSource = mainMultimediaList;
                            MultimediaRepeater.DataBind();
                            


                            videoPanel.Visible = true;
                            noVideoPanel.Visible = false;
                        }
                        // Musician does not have multimedia
                        else
                        {
                            videoPanel.Visible = false;
                            noVideoPanel.Visible = true;
                        }
                    }
                }
            }
        }
    }
    protected void additionalMultimediaClick(object sender, EventArgs e)
    {
        // Navigate to the current musician's multimedia.
        Session["uid"] = CurrentMusician.UID;                       
        Lutefisk.DirectPage(Page.Page, "Multimedia");                    
    }

    protected void profileClick(object sender, EventArgs e)
    {
        // Navigate to the current musicians profile.
        Session["uid"] = CurrentMusician.UID;
        Lutefisk.DirectPage(Page.Page, "Profile");                    
    }

    protected void multimediaClick(object sender, EventArgs e)
    {
        // Navigate to the current musician's multimedia.
        Session["uid"] = CurrentMusician.UID;
        Lutefisk.DirectPage(Page.Page, "Multimedia");
    }

    protected void curriculumVitaeClick(object sender, EventArgs e)
    {
        // Navigate to the current musician's cv.
        Session["uid"] = CurrentMusician.UID;
        Lutefisk.DirectPage(Page.Page, "CV");
    }
    protected void viewWebsiteClick(object sender, EventArgs e)
    {
        Lutefisk.DirectExternal(Page, CurrentMusician.Website);
    }
    protected void viewPDFClick(object sender, EventArgs e)
    {
        Lutefisk.DirectExternal(Page, CurrentMusician.CVLocation);
    }
}